package com.sxz.sbs.module.login;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sxz.sbs.R;
import com.sxz.sbs.app.App;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.component.ClearEditText;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.home.HomeActivity;
import com.sxz.sbs.module.login.presenter.LoginPresenter;
import com.sxz.sbs.module.login.view.LoginView;

import java.util.HashMap;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * @author hp
 * @date 2018/1/5
 * 登录界面
 */

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginView {


    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.et_login_phone)
    ClearEditText etLoginPhone;
    @BindView(R.id.et_login_psw)
    ClearEditText etLoginPsw;
    @BindView(R.id.checkbox_remember)
    CheckBox checkboxRemember;
    private static final String IS_REMEMBER_PWD = "is_remember_pwd";
    @Override
    protected void init() {
        if (!TextUtils.isEmpty(App.preferences.getValue(AppConstant.Preference.USER_NAME))) {
            etLoginPhone.setText(App.preferences.getValue(AppConstant.Preference.USER_NAME));
            if (App.preferences.getBooleanValue(IS_REMEMBER_PWD,false)) {
                checkboxRemember.setChecked(true);
                etLoginPsw.setText(App.preferences.getValue(AppConstant.Preference.USER_PSW));
            }
        }
        checkboxRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    App.preferences.setBooleanValue(IS_REMEMBER_PWD,true);
                } else {
                    App.preferences.setBooleanValue(IS_REMEMBER_PWD,false);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @OnClick({R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                String phone = etLoginPhone.getInputValue();
                if (!RegexUtils.isMobileExact(phone)) {
                    ToastUtils.showShort("请输入正确的手机号");
                    return;
                }
                String password = etLoginPsw.getInputValue();
                if (TextUtils.isEmpty(password)) {
                    ToastUtils.showShort("密码不能不空");
                    return;
                }
                HashMap<String, Object> map = HttpParamMap.getParamMap();
                map.put("account", phone);
                map.put("password", password);
                mPresenter.login(map);
                break;
            default:
                break;
        }
    }

    @Override
    public void onLoginSuccess(String token) {
        if (mIsLive) {
            if(JPushInterface.isPushStopped(this)){
                JPushInterface.resumePush(this);
            }
            JPushInterface.setAlias(this, etLoginPhone.getText().toString(), new TagAliasCallback() {
                @Override
                public void gotResult(int i, String s, Set<String> set) {
                    if (i == 0) {
                        App.preferences.setBooleanValue(AppConstant.Preference.JPUSH_SET_RESULT,
                                true);
                        //极光设置别名成功
                        LogUtils.e("极光设置别名成功");

                    } else {
                        App.preferences.setBooleanValue(AppConstant.Preference.JPUSH_SET_RESULT,
                                false);
                    }
                }
            });
            ToastUtils.showShort("登录成功");
            App.preferences.setValue(AppConstant.Preference.USER_NAME, etLoginPhone.getText().toString());
            App.preferences.setValue(AppConstant.Preference.USER_PSW, etLoginPsw.getText().toString());
            App.preferences.setValue(AppConstant.Preference.TOKEN, token);
            startActivity(HomeActivity.class);
            finish();
        }
    }


}
